<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 14:23
 */

namespace app\service;


use Redis\Redis;
use think\Db;

class RatioService
{
    public $Redis;
    function __construct()
    {
        $this->Redis=new Redis();
    }

    /**
     * @return bool|int|mixed|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 获取今日金币汇率
     */
    function getsTodayRatio()
    {
        $date=date("Y-m-d");
        $todayRatio=$this->Redis->hGet("ratio",$date);
        if(!empty($todayRatio)){
            return $todayRatio;
        }else{
            $info=Db::table("gold_ratio")->where("apply_time<='".$date."'")->order("apply_time desc")->find();
            if(!empty($info)){
                $_time=strtotime(date("Y-m-d 23:59:59"))-time();
                $this->Redis->hSet("ratio",$date,$info['ratio'],$_time);
                return $info['ratio'];
            }else{
                return 1;
            }
        }
    }

    /**
     * @return bool
     * @context 清空今日金币汇率
     */
    function delTodayRatio()
    {
        $date=date("Y-m-d");
        $this->Redis->hDel("ratio",$date);
        return true;
    }
}